package com.imay.live.activity.family.controller;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.imay.live.R;
import com.imay.live.activity.user.util.NickFilter;
import com.imay.live.manager.FamilyAccountManager;
import com.imay.live.manager.SensitiveWordFilterManager;
import com.imay.live.manager.UploadFileManager;
import com.imay.live.model.FamilyModel;
import com.imay.live.model.FamilyUserInfo;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.utils.GlideUtil;
import com.imay.live.utils.ToastUtil;
import com.imay.live.utils.ViewUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/8/29 0029.
 */

public class FamilyCreateController extends BaseFamilyController {

    protected String resultUrl;

    private final int count = 50;

    private ImageView sdv_family;
    private EditText et_family_name, et_family_nick, et_family_note;
    private TextView tv_family_create, tv_family_create_num;

    private InputMethodManager imm;

    @Override
    protected void initView() {
        sdv_family = ViewUtil.findView(activity, R.id.sdv_family);
        GlideUtil.loadImage(sdv_family, R.mipmap.ico_tianjiatouxiang);
        et_family_name = ViewUtil.findView(activity, R.id.et_family_name);
        NickFilter nickFilter = new NickFilter();
        InputFilter emojiFilter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Pattern emoji = Pattern.compile(
                        "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                        Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
                Matcher emojiMatcher = emoji.matcher(source);
                if (emojiMatcher.find()) {
                    return "";
                }
                return null;
            }
        };
        //NameLengthFilter filter = new NameLengthFilter(16);
        InputFilter[] namefilters = new InputFilter[]{emojiFilter, nickFilter, new InputFilter.LengthFilter(8)};
        et_family_name.setFilters(namefilters);
        //StringUtil.check(et_family_name);
        et_family_nick = ViewUtil.findView(activity, R.id.et_family_nick);
        //StringUtil.check(et_family_nick);
        InputFilter[] nickfilters = new InputFilter[]{emojiFilter, nickFilter, new InputFilter.LengthFilter(3)};
        et_family_nick.setFilters(nickfilters);
        et_family_note = ViewUtil.findView(activity, R.id.et_family_note);
        et_family_note.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.e("ttt", s.toString());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.e("ttt", s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
                tv_family_create_num.setText(activity.getString(R.string.str_family_create_note3, count - s.length()));
            }
        });
        tv_family_create_num = ViewUtil.findView(activity, R.id.tv_family_create_num);
        tv_family_create_num.setText(activity.getString(R.string.str_family_create_note3, count));
        tv_family_create = ViewUtil.findView(activity, R.id.tv_family_create);
        if (activity instanceof View.OnClickListener) {
            sdv_family.setOnClickListener((View.OnClickListener) activity);
            tv_family_create.setOnClickListener((View.OnClickListener) activity);
        }
        try {
            imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            ViewUtil.findView(activity, R.id.content).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (imm != null) {
                        imm.hideSoftInputFromWindow(et_family_name.getWindowToken(), 0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void hideName() {
        ViewUtil.findView(activity, R.id.ll_family_1).setVisibility(View.GONE);
        tv_family_create.setText(R.string.str_family_modify);
    }

    // 上传头像
    public void upload(String imagePath) {
        UploadFileManager.getInstance().uploadHead(imagePath, new UploadFileManager.UploadCallBack() {
            @Override
            public void uploadSuccess(String fileUrl) {
                setResultUrl(fileUrl);
                ToastUtil.show(activity, "上传成功");
            }

            @Override
            public void uploadFailer(int errorCode, String errorMessage) {
                ToastUtil.show(activity, "上传失败");
            }
        });
    }

    public void setResultUrl(String resultUrl) {
        this.resultUrl = resultUrl;
        GlideUtil.loadRectangleImage(sdv_family, resultUrl, R.dimen.radius_4x);
    }

    /**
     * 创建家族
     *
     * @return
     */
    public String createFamily() {
        if (SensitiveWordFilterManager.getInstance().hasSensitiveWord(et_family_name)
                || SensitiveWordFilterManager.getInstance().hasSensitiveWord(et_family_nick)
                || SensitiveWordFilterManager.getInstance().hasSensitiveWord(et_family_note)) {
            ToastUtil.show(activity, R.string.sensitive_comment);
            return null;
        }
        Editable name = et_family_name.getText();
        Editable nick = et_family_nick.getText();
        Editable note = et_family_note.getText();
        if (TextUtils.isEmpty(resultUrl)) {
            ToastUtil.show(activity, "封面必须有");
            return null;
        }
        if (TextUtils.isEmpty(name)) {
            ToastUtil.show(activity, "名称必须填");
            return null;
        }
        if (TextUtils.isEmpty(nick)) {
            ToastUtil.show(activity, "称号必须填");
            return null;
        }
        if (activity instanceof ProtoCallBack) {
            return ProtoRequestHelper.task(ProtoHttpFactory.createFamily(resultUrl, name.toString(), nick.toString(), note.toString())).setTag(activity.hashCode()).request((ProtoCallBack) activity);
        }
        return null;
    }

    /**
     * 修改家族
     *
     * @return
     */
    public String modifyFamily() {
        if (SensitiveWordFilterManager.getInstance().hasSensitiveWord(et_family_nick)
                || SensitiveWordFilterManager.getInstance().hasSensitiveWord(et_family_note)) {
            ToastUtil.show(activity, R.string.sensitive_comment);
            return null;
        }
        Editable nick = et_family_nick.getText();
        Editable note = et_family_note.getText();
        if (TextUtils.isEmpty(resultUrl)) {
            ToastUtil.show(activity, "封面必须有");
            return null;
        }
        if (TextUtils.isEmpty(nick)) {
            ToastUtil.show(activity, "称号必须填");
            return null;
        }
        if (FamilyAccountManager.getInstance().isHasFamilyInfo()) {
            if (activity instanceof ProtoCallBack) {
                return ProtoRequestHelper.task(ProtoHttpFactory.modifyFamily(FamilyAccountManager.getInstance().getFamilyUserInfo().getFamily().getId(), resultUrl, nick.toString(), note.toString())).setTag(activity.hashCode()).request((ProtoCallBack) activity);
            }
        }
        return null;
    }

    public void setData(FamilyUserInfo data, boolean focusable) {
        if (data != null) {
            FamilyModel family = data.getFamily();
            if (family != null) {
                if (!TextUtils.isEmpty(family.getUrl())) {
                    resultUrl = family.getUrl();
                    GlideUtil.loadRectangleImage(sdv_family, family.getUrl(), R.dimen.radius_4x);
                    sdv_family.setFocusable(focusable);
                    sdv_family.setEnabled(focusable);
                }

                if (family.getName() != null) {
                    et_family_name.setText(family.getName());
                    et_family_name.setSelection(family.getName().length());
                    et_family_name.setEnabled(focusable);
                    et_family_name.setFocusable(focusable);
                }

                if (family.getDesignation() != null) {
                    et_family_nick.setText(family.getDesignation());
                    et_family_nick.setSelection(family.getDesignation().length());
                    et_family_nick.setEnabled(focusable);
                    et_family_nick.setFocusable(focusable);
                }

                if (family.getAnnouncement() != null) {
                    et_family_note.setText(family.getAnnouncement());
                    et_family_note.setSelection(family.getAnnouncement().length());
                    et_family_note.setEnabled(focusable);
                    et_family_note.setFocusable(focusable);
                }
            }
        }
    }

    public void setButtom() {
        sdv_family.setFocusable(false);
        sdv_family.setEnabled(false);

        et_family_nick.setEnabled(false);
        et_family_nick.setFocusable(false);

        et_family_note.setEnabled(false);
        et_family_note.setFocusable(false);

        tv_family_create.setBackgroundResource(R.drawable.shape_corner100_70);
        tv_family_create.setText(R.string.str_family_check);
        tv_family_create.setClickable(false);
    }

    public void setEditData(FamilyUserInfo familyUserInfo, boolean focusable) {
        if (familyUserInfo != null) {
            FamilyModel family = familyUserInfo.getFamily();
            if (family != null) {
                if (!TextUtils.isEmpty(family.getEditUrl())) {
                    resultUrl = family.getEditUrl();
                    GlideUtil.loadRectangleImage(sdv_family, family.getEditUrl(), R.dimen.radius_4x);
                    sdv_family.setFocusable(focusable);
                    sdv_family.setEnabled(focusable);
                }

                if (family.getEditDesignation() != null) {
                    et_family_nick.setText(family.getEditDesignation());
                    et_family_nick.setSelection(family.getEditDesignation().length());
                    et_family_nick.setEnabled(focusable);
                    et_family_nick.setFocusable(focusable);
                }

                if (family.getEditAnnouncement() != null) {
                    et_family_note.setText(family.getEditAnnouncement());
                    et_family_note.setSelection(family.getEditAnnouncement().length());
                    et_family_note.setEnabled(focusable);
                    et_family_note.setFocusable(focusable);
                }
            }
        }
    }
}
